package silly.pack.timerthread;

import java.io.Console;
import java.util.Random;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.style.LineHeightSpan.WithDensity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

	AbsoluteLayout abllayout;
	ImageView image;
	ImageView troller;
	TextView txtsecond;
	TextView txtscore;
	Button btnstart;
	String strtime;
	AbsoluteLayout.LayoutParams location;
	AbsoluteLayout.LayoutParams location1;

	int second;
	int score;
	int screenwidth;
	int screenheight;
	float x;
	float y;
	float xtroll;
	float ytroll;
	float justificateX;
	float justificateY;
	float justificateXtroll;
	float justificateYtroll;
	int xdefault;
	int ydefault;
	int xdefaulttroll;
	int ydefaulttroll;
	int widthdefault;
	int heightdefault;
	int btnwidth;
	int btnheight;
	int level = 1;
	int speed = 1;

	boolean fuck;
	boolean check;
	boolean start;
	boolean fly = false;
	boolean troll = false;
	boolean flytroll = false;

	int count;
	int countclick;
	int limit;
	DisplayMetrics metric;
	Random random;

	Handler handlerimage = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			movebutton();
		}
	};

	private void movebutton() {
		// TODO Auto-generated method stub
		location.x = (int) x;
		location.y = (int) y;
		location.width = btnwidth;
		location.height = btnheight;
		image.setLayoutParams(location);
		if(troll == true){
			location1.x = (int) xtroll;
			location1.y = (int) ytroll;
			troller.setLayoutParams(location1);
		}
	}

	Handler handlertime = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if (second != 0) {
				txtsecond.setText(String.valueOf(second));
			} else {
				txtsecond.setText("Game Over");
			}
			if (second == 0) {
				GameOver();
			}
		}
	};

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		metric = this.getResources().getDisplayMetrics();
		screenwidth = metric.widthPixels;
		screenheight = metric.heightPixels;
		if (x<=0 || x + image.getWidth() >= screenwidth) {
			x = screenwidth - 1 - image.getWidth();
		}
		if (y<=0 || y + image.getHeight() >= screenheight) {
			y = screenheight - 1 - image.getHeight();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btnstart = (Button) findViewById(R.id.btnstart);
		abllayout = (AbsoluteLayout) findViewById(R.id.AbsoluteLayout1);
		txtsecond = (TextView) findViewById(R.id.txtsecond);
		txtscore = (TextView) findViewById(R.id.txtscore);
		image = (ImageView) findViewById(R.id.img);
		troller = (ImageView)findViewById(R.id.troll);
		location = (LayoutParams) image.getLayoutParams();
		location1 = (LayoutParams) troller.getLayoutParams();
		image.setVisibility(View.INVISIBLE);
		troller.setVisibility(View.INVISIBLE);
		txtsecond.setVisibility(View.INVISIBLE);

		metric = this.getResources().getDisplayMetrics();

		xdefault = location.x;
		ydefault = location.y;
		xdefaulttroll = location1.x;
		ydefaulttroll = location1.y;
		heightdefault = location.height;
		widthdefault = location.width;

		screenwidth = metric.widthPixels;
		screenheight = metric.heightPixels;

		random = new Random();

		Declaredefault();

		Thread newtimer = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (true) {
					Message msgtime = handlertime.obtainMessage();
					if (start == true) {
						second--;
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					handlertime.sendMessage(msgtime);
				}

			}
		});
		newtimer.start();

		Thread imagetimer = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (true) {
					Message msglocation = handlerimage.obtainMessage();
					if (start == true) {
						x += justificateX * speed;
						y += justificateY * speed;

						if (x <= 0 || x + image.getWidth() >= screenwidth) {
							justificateX = -justificateX;
						}
						if (y <= 0 || y + image.getHeight() >= screenheight) {
							justificateY = -justificateY;
						}
						if(troll == true){
							xtroll += justificateXtroll * speed;
							ytroll += justificateYtroll * speed;

							if (xtroll <= 0 || xtroll + image.getWidth() >= screenwidth) {
								justificateXtroll = -justificateXtroll;
							}
							if (ytroll <= 0 || ytroll + image.getHeight() >= screenheight) {
								justificateYtroll = -justificateYtroll;
							}
						}
						if (fly == true) {
							count++;
							if (count == limit) {
								count = 0;
								limit = random.nextInt(100) + 5;
								if(flytroll == true){
								Justificate();}
								else {
									troll =false;
									Justificate();
									troll = true;
								}
							}
						}
					}
					try {
						Thread.sleep(2);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					handlerimage.sendMessage(msglocation);
				}

			}
		});
		imagetimer.start();

		btnstart.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				image.setVisibility(View.VISIBLE);
				troller.setVisibility(View.INVISIBLE);
				btnstart.setVisibility(View.INVISIBLE);
				txtsecond.setVisibility(View.VISIBLE);
				image.setImageResource(R.drawable.mosquito);
				txtscore.setText("0");
				txtsecond.setText("10");
			}
		});

		troller.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				txtsecond.setText("Game Over");
				GameOver();
				Toast.makeText(getApplicationContext(), "You have been trolled", Toast.LENGTH_SHORT);
			}
		});
		
		image.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stubs
				score++;
				txtscore.setText(String.valueOf(score));
				countclick++;
				if (countclick == 3) {
					countclick = 0;
					second++;
				}
				if (start == false) {
					start = true;
				}

				btnwidth -= 40;
				btnheight -= 40;
				Justificate();

				if (btnwidth <= 80) {
					level++;
					switch (level) {
					case (10):
						speed = 1;
						fly = true;
						image.setImageResource(R.drawable.fly);
						break;
					case (20):
						speed = 1;
						fly = false;
						troll = true;
						image.setImageResource(R.drawable.mosquito);
						troller.setVisibility(View.VISIBLE);
						break;
					case (30):
						speed = 1;
						fly = true;
						troll = true;
						image.setImageResource(R.drawable.fly);
						break;
					case (40):
						speed = 1;
						fly = true;
						flytroll = true;
						troll = true;
						image.setImageResource(R.drawable.fly);
						break;
					default:
						speed++;
						break;
					}
					Declaredefault();
					movebutton();
					Toast.makeText(getApplicationContext(),
							"You win \nStart level " + level,
							Toast.LENGTH_SHORT).show();
				}
			}

		});

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	private void Declaredefault() {
		x = xdefault;
		y = ydefault;
		if(troll==true){
			xtroll = xdefaulttroll;
			ytroll = ydefaulttroll;
		}
		justificateX = 0;
		justificateY = 0;
		justificateXtroll = 0;
		justificateYtroll = 0;
		btnwidth = widthdefault;
		btnheight = heightdefault;
		second = 10 + speed * 2 - 1;

		start = false;

		count = 0;
		countclick = 0;
		if (fly == true) {
			limit = random.nextInt(100) + 5;
		}
	}

	private void Justificate() {
		fuck = random.nextBoolean();
		justificateX = random.nextFloat();
		if (fuck == true) {
			justificateX = -justificateX;
		}
		fuck = random.nextBoolean();
		justificateY = (float) Math.sqrt((double) (1 - justificateX
				* justificateX));
		if (fuck == true) {
			justificateY = -justificateY;
		}
		if(troll == true){
			fuck = random.nextBoolean();
			justificateXtroll = random.nextFloat();
			if (fuck == true) {
				justificateXtroll = -justificateXtroll;
			}
			fuck = random.nextBoolean();
			justificateYtroll = (float) Math.sqrt((double) (1 - justificateXtroll
					* justificateXtroll));
			if (fuck == true) {
				justificateYtroll = -justificateYtroll;
			}
		}
	}
	
	private void GameOver() {
		// TODO Auto-generated method stub
		level = 1;
		score = 0;
		speed = 1;
		fly = false;
		troll = false;
		flytroll = false;
		Declaredefault();
		movebutton();
		btnstart.setText("Retry");
		btnstart.setVisibility(View.VISIBLE);
		image.setVisibility(View.INVISIBLE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}
